/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit.console.parser;

import com.ssm.changeunit.command.*;
import org.apache.commons.cli.*;

/**
 * Console에서 입력한 Command를 파싱하는 클래스
 * 
 * @author 시리우스
 */
public class LogConsoleParser extends ConsoleParser {
    /**
     * @param userCommand
     * @return
     * @throws com.ssm.changeunit.console.parser.ConsoleParseException
     */
    @Override
    public CommandWorkerOptions parse(java.lang.String[] userCommand) throws ConsoleParseException {
       try {
            // 파서 생성
            CommandLineParser parser = new GnuParser();

            Options options = new Options();

            Option logOption = OptionBuilder.withLongOpt("file").hasArgs().create();

            options.addOption(logOption);

            CommandLine commandLine = parser.parse(options, userCommand);

            CommandWorkerOptions commandWorkerOptions = new CommandWorkerOptions();
            
            // Log 프로퍼티가 있는지 검사
            if(commandLine.hasOption("file")) {
                CommandWorkerOption commandWorkerOption = new CommandWorkerOption("file");
                
                commandWorkerOptions.addCommandWorkerOption(commandWorkerOption);
                
                String file = commandLine.getOptionValue("file");

                commandWorkerOption.addValue(file);
                
                return commandWorkerOptions;
            }
            
            return commandWorkerOptions;
        }
        catch(ParseException ex) {
            throw new ConsoleParseException(ex);
        }
    }
}